﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using DotSpatial.Topology;

namespace HAMath
{

    /// <summary>
    /// 判断一个面与多边形是否有交集
    /// </summary>
   public   class SurfaceInPolygonRegion
    {


      //Polygon 

       private static List<Coordinate> surfacePoint;
       private static List<Coordinate> polygonPoint;


       private static Polygon surface;
       private static Polygon polygon;





       /// <summary>
       /// 判断多边形是否和多边形有交集 :当面中的线段相互交叉的时候，求交集面积的方法会出错。
       /// 参数的选择很纠结，主要是为了通用性  哈哈
       /// </summary>
       /// <param name="surfacePointCoordinate">构成面的顶点的集合 数组中按照XYZ的顺序存放点的坐标信息</param>
       /// <param name="polygonPoingCoornidate">构成多边形的顶点的集合 数组中按照XYZ的顺序存放点的坐标信息</param>
       /// <returns></returns>
       public static IntersectPolygonResult JudgePolygonIntersectionPolygon(List<double[]> surfacePointCoordinate, List<double[]> polygonPoingCoornidate)
       //public static bool JudgePolygonIntersectionPolygon(List <double [ ]> surfacePointCoordinate,List <double []> polygonPoingCoornidate)
       {


           IntersectPolygonResult intersectPolygonResult = new IntersectPolygonResult();

           try
           {


               #region 首先是数据的有效性检验

               if (surfacePointCoordinate == null || polygonPoingCoornidate == null)
               {
                   return intersectPolygonResult;
               }
               if (surfacePointCoordinate.Count < 1 || polygonPoingCoornidate.Count < 1)
               {
                   return intersectPolygonResult;
               }
               for (int i = 0; i < surfacePointCoordinate.Count; i++)
               {
                   if (surfacePointCoordinate[i].GetLength(0) != 3)
                   {
                       return intersectPolygonResult;
                   }
               }
               for (int i = 0; i < polygonPoingCoornidate.Count; i++)
               {
                   if (polygonPoingCoornidate[i].GetLength(0) != 3)
                   {
                       return intersectPolygonResult;
                   }
               }

               #endregion


               #region 面和多边形的构造

               surfacePoint = new List<Coordinate>();

               //添加面的点的坐标
               for (int i = 0; i < surfacePointCoordinate.Count; i++)
               {
                   Coordinate coor = new Coordinate();

                   coor.X = surfacePointCoordinate[i][0];
                   coor.Y = surfacePointCoordinate[i][1];
                   coor.Z = surfacePointCoordinate[i][2];

                   surfacePoint.Add(coor);

               }

               polygonPoint = new List<Coordinate>();


               //添加多边形的点的坐标
               for (int i = 0; i < polygonPoingCoornidate.Count; i++)
               {
                   Coordinate coor = new Coordinate();

                   coor.X = polygonPoingCoornidate[i][0];
                   coor.Y = polygonPoingCoornidate[i][1];
                   coor.Z = polygonPoingCoornidate[i][2];

                   polygonPoint.Add(coor);

               }

               surface = new Polygon(new LinearRing(surfacePoint));//构建面多边形
               polygon = new Polygon(new LinearRing(polygonPoint));//构建多边形

               #endregion


               intersectPolygonResult.IsInPolygon = polygon.Intersects(surface);

               //下面计算多边形和多边形相交的面积

               if (intersectPolygonResult.IsInPolygon == true)
               {

                   //intersectPolygonResult.ResultValue = (polygon.SymmetricDifference(surface)).Difference(polygon).Area;//常量0
                   //intersectPolygonResult.ResultValue = (surface.SymmetricDifference(polygon)).Difference(polygon).Area;//常量0
                   //intersectPolygonResult.ResultValue = (polygon.SymmetricDifference(surface)).SymmetricDifference(surface).Area;//常量125

                   //intersectPolygonResult.ResultValue = (surface.SymmetricDifference(polygon)).Difference(surface).Area;//相对于polygon的去处和surface的相交部分的面积
                   //intersectPolygonResult.ResultValue = (polygon.SymmetricDifference(surface)).Difference(surface).Area;//相对于polygon的去处和surface的相交部分的面积

                   //surface的内部线段相交的情况会出错？？？
                   intersectPolygonResult.ResultValue = surface.SymmetricDifference(polygon).SymmetricDifference(polygon.Union(surface)).Area;//相对于polygon的和surface的相交部分的面积

               }
               else
               {
                   intersectPolygonResult.ResultValue = 0.0;
               }

               return intersectPolygonResult;

               //return _isPolygonIntersectPolygon;

           }
           catch
           {
               
               intersectPolygonResult.IsInPolygon = false;
               intersectPolygonResult.ResultValue = 0.0;
               return intersectPolygonResult;
           }




       }




       //public static bool JudgePolygonIntersectionPolygon(Polygon surface,Polygon polygon)
       //{

       //    try
       //    {

       //        if (surface == null)
       //        {
       //            return false;
       //        }
       //        if (polygon == null)
       //        {
       //            return false;
       //        }


       //        return polygon.Intersects(surface);

       //        //polygon.Intersection(surface );

       //        //return true;
       //    }
       //    catch (Exception)
       //    {

       //        return false;
       //    }

         


       //}





    }
}
